package com.wangsd.web.face.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.MyController;
import com.wangsd.web.face.entity.MemberLevel;
import com.wangsd.web.face.service.MemberLevelService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 会员等级会员等级控制
 * </p>
 *
 * @author generator
 * @since 2019-10-31
 */
@Api(value = "memberLevel", tags = "会员等级")
@RestController
@RequestMapping("/face/memberLevel")
@Slf4j
public class MemberLevelController extends MyController {

    @Autowired
    MemberLevelService memberLevelService;


    @ApiOperation(value = "查询会员等级列表", notes = "查询会员等级列表", httpMethod = "POST")
    @RequestMapping(value = "/selectList/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Result<IPage<MemberLevel>> selectList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                                 @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                                 @RequestBody @ApiParam(name = "params", value = "传入json格式") MemberLevel params) {
        SysUser loginUser = getUserInfo();
        IPage<MemberLevel> resultIPage = memberLevelService.selectList(pageNum, pageSize, params, loginUser.getCode());
        return Result.success(resultIPage);
    }


    @ApiOperation(value = "新增或修改会员等级", notes = "新增或修改会员等级", httpMethod = "POST", response = Result.class)
    @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST)
    public Result addOrUpdate(MemberLevel params) {
        if (params.getMerchantId() == null) {
            return Result.fail("商户必填！");
        }
        if (params.getDiscount() == null) {
            return Result.fail("会员折扣必填！");
        }
        if (params.getDiscount() < 0 || params.getDiscount() > 1) {
            return Result.fail("会员折扣必须在0到1之间！");
        }
        if (params.getOnceFee() == null) {
            return Result.fail("单次充值多少达标必填！");
        }
        if (params.getIsDefault() == null) {
            return Result.fail("注册默认等级必填！");
        }
        if (params.getCardName() == null) {
            return Result.fail("会员卡名称必填！");
        }
        if (params.getIsDefault() == 1) {
            Integer count = memberLevelService.selectCountMerchantId(1, params.getMerchantId());
            if (count >= 1) {
                return Result.fail("默认等级只能存在一个");
            }
        }
        MemberLevel memberLevel = memberLevelService.selectIsDefaultAndMerchantId(1, params.getMerchantId());
        params.setCode(memberLevel.getCode());
        memberLevelService.addOrUpdate(params);
        return Result.success();
    }

    @ApiOperation(value = "删除会员等级信息", notes = "删除会员等级信息", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result delete(@ApiParam(value = "用户id", required = true) @PathVariable("id") Long id) {
        memberLevelService.removeById(id);
        return Result.success();
    }

}

